package com.qf.web;


import com.qf.pojo.Categorp;
import com.qf.pojo.Product;
import com.qf.service.CategorpService;
import com.qf.service.ProductService;
import com.qf.service.impl.CategorpServiceImpl;
import com.qf.service.impl.ProductServiceImpl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ProductApp {
    //商品信息
    public  static ProductService productService=new ProductServiceImpl();
    //商品类别信息
    public static CategorpService categorpService=new CategorpServiceImpl();

    public static void main(String[] args) {

        // 当前是一个客户端入口
              //  Scanner scanner = new Scanner(System.in);

        try {
            //键盘录入缓冲流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while (true){
                System.out.println("请输入以下命令进行操作");
                System.out.println("FP.查看商品信息   FC.查看商品类别信息  Q.退出");
                String c = bufferedReader.readLine();
                switch (c){
                    case "FP":     //商品信息
                        while (true){
                            System.out.println("欢迎进入商品信息查询系统，请输入以下命令进行操作：");
                            System.out.println("C:创建 U:修改 D:删除 FA:查询所有 FI:根据id查询 DA:根据id批量删除 Q:退出");
                            String s = bufferedReader.readLine();
                            switch (s.toUpperCase()) {
                                case "C":
                                    // System.out.println("创建");
                                    createProduct(bufferedReader);
                                    break;
                                case "U":
                                    //System.out.println("修改");
                                    updateProduct(bufferedReader);
                                    break;
                                case "D":
                                    //System.out.println("删除");
                                    deleteProductById(bufferedReader);
                                    break;
                                case "FA":
                                    //执行查询，返回对应的结果
                                    //通过模拟服务端的方式执行
                                    // System.out.println("查询所有");
                                    findAllProduct();
                                    break;
                                case "FI":
                                    //System.out.println("根据id查询");
                                    //根据Id查询接口
                                    findById(bufferedReader);
                                    break;
                                case "DA":
                                    //System.out.println("根据id批量删除");
                                    deletesProduct(bufferedReader);
                                    break;
                                case "Q":
                                    System.out.println("退出商品信息系统");
                                    System.exit(1);
                                    System.out.println("Bye~");
                                    break;
                                default:
                                    System.out.println("输入有误，请重试");
                                    break;
                            }
                            if(c.equals("Q")){
                                break;
                            }
                        }
                        break;
                    case "FC":      //商品类别信息
                        while (true){
                            System.out.println("欢迎进入商品类别信息查询系统，请输入以下命令进行操作：");
                            System.out.println("CC：创建、CU：修改、CD：根据id删除、CDS：批量删除、CF：通过id查询、CFS：查询所有、Q：退出");
                            String cc = bufferedReader.readLine();
                            switch (cc){
                                case "CC":
                                    //创建
                                    addCategorp(bufferedReader);
                                    break;
                                case "CU":
                                    //修改

                                    updateCategorpByCId(bufferedReader);
                                    break;
                                case "CD":
                                    //根据id删除
                                    deleteCategorp(bufferedReader);
                                    break;
                                case "CDS":
                                    //批量删除
                                    deleteCategorps(bufferedReader);
                                    break;
                                case "CF":
                                    //根据id查询
                                    findCategorpByCId(bufferedReader);

                                    break;
                                case "CFS":
                                    //查询所有

                                    findAll();
                                    break;
                                case "Q":
                                    //退出
                                    System.out.println("退出商品类别信息系统");
                                    System.out.println("Bye~");
                                    break;
                                default:
                                    System.out.println("输入有误，请重试");
                                    break;
                            }
                            if (cc.equals("Q")){
                                break;
                            }
                        }
                        break;
                    case "Q":
                        System.out.println("Bye~，下次见");
                        break;
                    default:
                        System.out.println("输入有误，请重试");
                        break;
                        }
                    }
        } catch (Exception e) {
            System.out.println("系统维护中，请稍后再试");
           // e.printStackTrace();
        }
    }
    /**
     * @Description: 商品类别信息
     * @Param:
     * @return:
     * @Author: shengchunquan
     * @Date: 2022/9/20 19:27
     */


    //批量删除商品信息
    public static void deleteCategorps(BufferedReader bufferedReader){
        try {
            //定义一个集合用于接收输入的id
            List<String> list =new ArrayList<>();
            while (true){
                System.out.println("请输入你要删除的id，输入y代表结束");
                //获取输入的数据
                String id = bufferedReader.readLine();
                if("y".equals(id)){
                    break;
                }else {
                    list.add(id);
                }
            }
            //删除
          categorpService.deleteCategorps(list);
            System.out.println("批量删除成功");
        }catch (Exception e){
            System.out.println("输入有误，请重试");
            return;
        }
    }


    //查询所有商品类别信息
    public static void findAll() {
        List<Categorp> list = categorpService.findAll();
        for (Categorp c : list) {
            System.out.println(c);
        }
    }



    //根据cid删除商品类别
    public static void deleteCategorp(BufferedReader bufferedReader){
        try {
            System.out.println("请输入你要删除的商品类别编号：");
            String cid = bufferedReader.readLine();
            //通过Id查询这个要删除的对象是否存在
            Categorp categorp= categorpService.findCategorpByCId(cid);
            if(categorp!=null){
                //执行删除语句
                int n = categorpService.deleteCategorp(cid);
                if(n>0){
                    System.out.println("商品类别删除成功！");
                }else {
                    System.out.println("商品类别删除失败，请重试!");
                }
            }else {
                System.out.println("要删除的商品不存在");
            }
        } catch (IOException e) {
            System.out.println("输入信息有误，请重试");
        }
    }



    //修改商品类别信息
    public static void updateCategorpByCId(BufferedReader bufferedReader){
        try {
            System.out.println("请输入你要修改的商品类别编号：");
            String cid = bufferedReader.readLine();
            //通过Id查询这个要修改的对象是否存在
            Categorp categorp= categorpService.findCategorpByCId(cid);
            if(categorp!=null){
                System.out.println("请输入要修改的商品类别名称：");
                String cname = bufferedReader.readLine();
                //赋值用于修改
                Categorp cc = new Categorp(cid,cname);
                //执行修改语句
                int n = categorpService.updateCategorpByCId(cc);
                if (n>0){
                    System.out.println("商品类别信息修改成功！");
                }else{
                    System.out.println("商品类别信息修改失败");
                }
            }else {
                System.out.println("你要修改商品信息的类别不存在");
            }
        } catch (IOException e) {
            System.out.println("输入信息有误，请重试");
        }

    }


    //根据商品类别编号查询信息
    public static void findCategorpByCId(BufferedReader bufferedReader){
        try {
            System.out.println("请输入要查询的商品类别编号：");
            String cid = bufferedReader.readLine();
            Categorp categorp=categorpService.findCategorpByCId(cid);
            if (categorp!=null){
                System.out.println(categorp);
            }else {
                System.out.println("查询的数据不存在");
            }
        } catch (IOException e) {
            System.out.println("输入信息有误，请重试");
        }
    }


    //添加商品类别信息
    public static void addCategorp(BufferedReader bufferedReader){
        try {
            System.out.println("请输入商品类别编号：");
            String cid = bufferedReader.readLine();
            Categorp c = categorpService.findCategorpByCId(cid);//通过id查询这个对象

            if(c!=null){
                System.out.println(c);
                System.out.println("该商品编号已存在");
            }else {
                //要添加的
                System.out.println("请输入商品类别：");
                String cname = bufferedReader.readLine();
                Categorp categorp=new Categorp(cid,cname);
                int n=categorpService.addCategorp(categorp);
                if (n>0){
                    System.out.println("商品类别添加成功！");
                }else {
                    System.out.println("商品类别添加失败，请重试");
                }
            }
        } catch (IOException e) {
            System.out.println("输入信息有误，请重试");
        }
    }

    /**
     * @Description: 商品信息
     * @Param:
     * @return:
     * @Author: shengchunquan
     * @Date: 2022/9/20 19:29
     */

    //添加商品
    public static void createProduct(BufferedReader bufferedReader){
        try {
            System.out.println("请输入商品信息");
            System.out.println("请输入商品的名称");
            String pname = bufferedReader.readLine();
            System.out.println("请输入商品价格：");
            String price1 = bufferedReader.readLine();
            int price = Integer.parseInt(price1);
            System.out.println("请选择商品是否上架：1.上架，2.下架");
            String f = bufferedReader.readLine();
            int flag = Integer.parseInt(f);

            //查询所有的商品分类
            List<Categorp> all=categorpService.findAll();
            // cname : cid
            Map<String,String> map = new HashMap<>();
            Map<String,String> map1 = new HashMap<>();
            StringBuilder stringBuilder =  new StringBuilder();
            for (int i = 0; i < all.size(); i++) {
                map.put(all.get(i).getCname(),all.get(i).getCid());
                map1.put(i+1+"",all.get(i).getCname());
                stringBuilder.append(i+1).append(".").append(all.get(i).getCname()).append(" \t");
            }
            System.out.println("请选择商品分类，输入编号");
            System.out.println(stringBuilder.toString());
            //用户输入对应的编号
            String id = bufferedReader.readLine();
            //根据Key获取对应的name值
            String cname=map.get(id);
            //获取对应的id
            String cid=map.get(cname);
            Product product = new Product();
            product.setPname(pname);
            product.setPrice(price);
            product.setCid(cid);
            product.setFlag(flag);
            int count=productService.createProduct(product);
            if(count>0){
                System.out.println("添加成功");
            }
        } catch (IOException e) {
            System.out.println("输入信息有误，请重试");
            return;
        }
    }

    //批量删除商品信息
    public static void deletesProduct(BufferedReader bufferedReader){
            ProductService productService=new ProductServiceImpl();
            try {
                //定义一个集合用于接收输入的id
                List<Integer> list =new ArrayList<>();
                while (true){
                    System.out.println("请输入你要删除的id，输入y代表结束");
                    //获取输入的数据
                    String i = bufferedReader.readLine();
                    if("y".equals(i)){
                        break;
                    }else {
                        int pid = Integer.parseInt(i);
                   list.add(pid);
                    }
                }
                //删除
                productService.deleteProductes(list);
                        System.out.println("批量删除成功");

            }catch (Exception e){
                //e.printStackTrace();
                System.out.println("输入有误，请重试");
                return;
            }
    }


    //删除一条商品信息
    public static void deleteProductById(BufferedReader bufferedReader){
        //调用业务层
        ProductService productService=new ProductServiceImpl();
        try {
            System.out.println("请输入要删除的商品id：");
            //获取id
            String id = bufferedReader.readLine();
            //对id进行校验
            int pid = Integer.parseInt(id);
            //调用业务层执行sql语句(执行查询)
            Product product=productService.findById(pid);
            //执行判断
            if (product!=null){
                int count=productService.deleteProductById(pid);
                if(count>0){
                    System.out.println("删除成功");
                }else {
                    System.out.println("删除失败，请重试");
                }
            }else {
                System.out.println("查询失败，请重试");
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("输入有误，请重新操作");
        }
    }

    //修改商品信息
    public static void updateProduct(BufferedReader bufferedReader){
        //调用业务层
        ProductService productService=new ProductServiceImpl();
        try {
            System.out.println("请输入你要查询的商品id");
            //获取id
            String id = bufferedReader.readLine();
            //对输入的id进行校验
            int pid = Integer.parseInt(id);
            //调用service执行sql语句
            Product product= productService.findById(pid);
            //执行判断
            if (product!=null) {
                System.out.println("请输入商品名称：");
                String pname = bufferedReader.readLine();
                product.setPname(pname);
                System.out.println("请输入商品价格：");
                String pr = bufferedReader.readLine();
                int price = Integer.parseInt(pr);
                product.setPrice(price);
                System.out.println("输入是否上架 1代表上架 0代表下架");
                String f = bufferedReader.readLine();
                int flag = Integer.parseInt(f);
                product.setFlag(flag);
                //执行修改
                int count = productService.UpdateProduct(product);
                if (count > 0) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败，请重试");
                }
             }else {
                System.out.println("查询失败，请重新查询");
                return;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("输入有误，请重新操作");
            return;
        }
    }
    //查询商品信息
     private static void findById(BufferedReader bufferedReader){
         try {
             ProductService productService=new ProductServiceImpl();

             System.out.println("请输入你要查询的商品id");
             //获取id
             String id = bufferedReader.readLine();
             //对输入的id进行校验
             int pid = Integer.parseInt(id);
             Product product= productService.findById(pid);
             System.out.println("商品id"+"\t"+"商品名称"+"\t"+"商品价格"+"\t"+"是否上架"+"\t"+"商品分类");
             System.out.println(product.getPid()+
                     "\t"+product.getPname()+
                     "\t"+product.getPrice()+"\t"+
                     (product.getFlag()==1?"上架":"下架")+
                     "\t"+product.getCid());

         } catch (IOException e) {
             System.out.println("输入有误，请重新操作");
             return;
         }
     }


   //查询所有
    private static void findAllProduct() {
        //调用业务层
        ProductService productService =  new ProductServiceImpl();
        List<Product> list = productService.findAll();
        //查询结果
        System.out.println("查询结果");
        for (Product product : list) {
            System.out.println(product);
        }
    }
}